#!/usr/bin/env python

from Genedge import Genedge
from io.GenInput import GenInput
import random
from graph.SuperGraph import SuperGraph

class GenGraph:
    "Wrapper for graph. Adds startshape and background information"
    
    def __init__(self, name):
        self.grapho = SuperGraph(name)
        self.startshape = ""
        self.background = []
        self.score = 0
    
    def __getitem__(self,n):
        """Return the neighbors of node n as a list.
        This provides graph G the natural property that G[n] returns
        the neighbors of G. 
        """
        try:
            return self.grapho.multinodes.get(n)
        except KeyError:
            raise GraphError, "node %s not in graph"%(n,)
    
    def name(self):
        return str(self.grapho)
    
    def set_name(self, name):
        self.grapho.name = name
    
    def subgraph(self, node, max_instances, max_distance):
        return self.grapho.subgraph(node, max_instances, max_distance)
    
    def add_node(self, node):
        self.grapho.add_node(node)
    
    def add_edge(self, start, finish, edge, instance):
        return self.grapho.add_edge(start, finish, edge, instance)
    
    def remove_node(self, mnode):
        return self.grapho.remove_node(mnode)
    
    def set_startshape(self, startshape):
        self.startshape = startshape
    
    def repair_ss(self):
        if self.startshape not in self.grapho.nodes():
            self.startshape = random.choice(self.grapho.nodes())
    
    def set_background(self, params):
        self.background = params
    
    def nodes(self):
        return self.grapho.nodes()
    
    def nodes_iter(self):
        return self.grapho.nodes_iter()
    
    def edges_iter(self):
        return self.grapho.edges_iter()
    
    def out_edges(self):
        return self.grapho.out_edges()
    
    def combine(self, new_graph, eq_table):
        return self.grapho.combine(new_graph, eq_table)
    
    def bfs(self, start, graph):
        return self.grapho.bfs(start, graph)
    
    def has_background(self):
        if len(self.background) is not 0:
            return True
        else:
            return False
    
    def print_graph(self):
        return self.grapho.print_graph()
        
    def __contains__(self,n):
        """Return True if n is a node in graph.
        Allows the expression 'n in G'.
        Testing whether an unhashable object, such as a list, is in the
        dict datastructure (self.adj) will raise a TypeError.
        Rather than propagate this to the calling method, just
        return False.
        """
        try:
            return n in self.grapho.multinodes
        except TypeError:
            return False
    
    def __str__(self):
        return str(self.grapho)